Page({
  data: {
    sugarOptions: ['无糖', '微糖', '半糖', '少糖', '全糖'],
    sugarIndex: 2,
    iceOptions: ['热饮', '常温', '少冰', '正常冰', '多冰'],
    iceIndex: 2,
    typeOptions: ['奶茶', '果茶', '咖啡', '其他'],
    typeIndex: 0,
    rating: 5,
    shopOptions: ['喜茶', '奈雪的茶', '一点点', '蜜雪冰城', 'COCO都可', '贡茶', '鹿角巷', '茶百道', '其他'],
    shopIndex: 0,
    popularShops: ['喜茶', '奈雪的茶', '一点点', '蜜雪冰城', 'COCO都可', '贡茶', '鹿角巷', '茶百道'],
    selectedShop: '喜茶',
    customShopName: ''
  },
  
  // 分享给好友
  onShareAppMessage() {
    return {
      title: '记录你的奶茶时光，添加新的奶茶体验',
      path: '/pages/addTea/addTea',
    };
  },

  // 分享到朋友圈
  onShareTimeline() {
    return {
      title: '记录每一杯奶茶，留住甜蜜时光',
      query: 'from=timeline',
    };
  },
  
  onSugarChange(e) {
    this.setData({
      sugarIndex: e.detail.value
    });
  },
  
  onIceChange(e) {
    this.setData({
      iceIndex: e.detail.value
    });
  },
  
  onTypeChange(e) {
    this.setData({
      typeIndex: e.detail.value
    });
  },
  
  onShopChange(e) {
    const index = parseInt(e.detail.value);
    let selectedShop = this.data.shopOptions[index];
    
    // 如果不是"其他"，直接设置为选中的店铺
    if (index !== this.data.shopOptions.length - 1) {
      this.setData({
        shopIndex: index,
        selectedShop: selectedShop
      });
    } else {
      // 如果是"其他"，清空自定义输入
      this.setData({
        shopIndex: index,
        selectedShop: '',
        customShopName: ''
      });
    }
  },
  
  // 点击快捷选择店铺标签
  selectShop(e) {
    const shop = e.currentTarget.dataset.shop;
    this.setData({
      selectedShop: shop
    });
    
    // 找到对应的索引并更新
    const index = this.data.shopOptions.findIndex(item => item === shop);
    if (index !== -1) {
      this.setData({
        shopIndex: index
      });
    }
  },
  
  // 自定义店铺名称输入
  onCustomShopInput(e) {
    this.setData({
      customShopName: e.detail.value
    });
  },
  
  setRating(e) {
    const rating = e.currentTarget.dataset.rating;
    this.setData({
      rating
    });
  },
  
  formSubmit(e) {
    const formData = e.detail.value;
    
    // 获取当前日期作为记录日期
    const date = new Date();
    const year = date.getFullYear();
    const month = date.getMonth() + 1;
    const day = date.getDate();
    const dateStr = `${year}/${month}/${day}`;
    const displayDateStr = `${year}年${month}月${day}日`;
    
    // 确定店铺名称
    let shopName;
    if (this.data.shopIndex === this.data.shopOptions.length - 1) {
      // 如果选择了"其他"，使用自定义输入的店铺名称
      shopName = formData.customShop || '未知店铺';
    } else if (this.data.selectedShop) {
      // 使用选中的店铺名称
      shopName = this.data.selectedShop;
    } else {
      // 默认使用下拉选择的店铺
      shopName = this.data.shopOptions[this.data.shopIndex];
    }
    
    // 补充表单数据
    formData.shop = shopName;
    formData.sugar = this.data.sugarOptions[this.data.sugarIndex];
    formData.ice = this.data.iceOptions[this.data.iceIndex];
    formData.type = this.data.typeOptions[this.data.typeIndex];
    formData.rating = this.data.rating;
    formData.date = dateStr;
    formData.displayDate = displayDateStr;
    formData.id = Date.now(); // 使用时间戳作为唯一ID
    formData.typeIcon = `/images/tea-type${this.data.typeIndex + 1}.png`;
    
    console.log('提交的奶茶记录:', formData);
    
    // 从本地存储中获取现有记录
    let teaRecords = wx.getStorageSync('teaRecords') || [];
    
    // 添加新记录
    teaRecords.push(formData);
    
    // 保存回本地存储
    wx.setStorageSync('teaRecords', teaRecords);
    
    // 更新统计数据
    this.updateStatistics(teaRecords);
    
    // 提示用户
    wx.showToast({
      title: '记录已保存',
      icon: 'success',
      duration: 2000
    });
    
    // 返回上一页
    setTimeout(() => {
      wx.navigateBack({
        delta: 1
      });
    }, 1500);
  },
  
  // 更新统计数据
  updateStatistics(records) {
    // 当前月份
    const currentDate = new Date();
    const currentYear = currentDate.getFullYear();
    const currentMonth = currentDate.getMonth() + 1;
    
    // 计算总记录数
    const totalRecords = records.length;
    
    // 计算当月记录数
    const thisMonthRecords = records.filter(record => {
      const dateParts = record.date.split('/');
      return parseInt(dateParts[0]) === currentYear && parseInt(dateParts[1]) === currentMonth;
    }).length;
    
    // 计算最爱店铺
    const shopCounts = {};
    records.forEach(record => {
      if (record.shop) {
        shopCounts[record.shop] = (shopCounts[record.shop] || 0) + 1;
      }
    });
    
    let favoriteShop = '无';
    let maxCount = 0;
    
    for (const shop in shopCounts) {
      if (shopCounts[shop] > maxCount) {
        maxCount = shopCounts[shop];
        favoriteShop = shop;
      }
    }
    
    // 保存统计数据
    wx.setStorageSync('teaStatistics', {
      totalRecords,
      thisMonthRecords,
      favoriteShop
    });
  }
}); 